package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台精彩瞬间Controller
 *
 * @author jjp
 * @date 2020-04-14
 */
@RestController
@Api(description = "平台精彩瞬间")
@RequestMapping("/mobileUser/moment")
public class UserMomentController extends BaseController {
    @Autowired
    private ISysMomentService sysMomentService;

    @Autowired
    private ISysMomentFileService sysMomentFileService;

    @Autowired
    private ISysMomentCommentService sysMomentCommentService;

    @Autowired
    private ISysPariseService sysPariseService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询平台精彩瞬间列表
     */
    @ApiOperation("查看所有精彩瞬间")
    @GetMapping("/list")
    public TableDataInfo list(SysMoment sysMoment) {
        startPage();
        List<SysMoment> list = sysMomentService.selectSysMomentList(sysMoment);
        for (int i = 0; i < list.size(); i++) {
            String userName = list.get(i).getCreateBy();
            SysUser sysUser = sysUserService.selectUserGetUserName(userName);
            list.get(i).setSysUser(sysUser);
            Long momentId = list.get(i).getMomentId();
            List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
            list.get(i).setSysMomentFile(sysMomentFiles);
        }
        return getDataTable(list);
    }


    /**
     * 获取平台精彩瞬间详细信息
     * <p>
     * APP接口
     */
    @ApiOperation("查看单个精彩瞬间")
    @GetMapping(value = "/getMomentInfo/{momentId}")
    public AjaxResult getMomentInfo(@PathVariable("momentId") Long momentId) {
        SysMoment sysMoment = sysMomentService.selectSysMomentById(momentId);
        String userName = sysMoment.getCreateBy();
        SysUser sysUser = sysUserService.selectUserGetUserName(userName);
        sysMoment.setSysUser(sysUser);
        List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
        List<SysMomentComment> sysMomentComments = sysMomentCommentService.selectSysMomentId(momentId);
        for (int i = 0; i < sysMomentComments.size(); i++) {
            Long commentId = sysMomentComments.get(i).getCommentId();
            List<SysMomentComment> sysMomentCommentList = sysMomentCommentService.selectMomentCommentByParentCommentId(commentId);
            sysMomentComments.get(i).setSysMomentComment(sysMomentCommentList);

            sysMomentComments.get(i).setCommentCount(sysMomentCommentList.size());

            SysParise sysParise = new SysParise();
            sysParise.setMomentCommentId(sysMomentComments.get(i).getCommentId());
            Integer momentCommentPariseCount = sysPariseService.selectCount(sysParise);
            sysMomentComments.get(i).setMomentCommentPariseCount(momentCommentPariseCount);

            Long mobileUserId = sysMomentComments.get(i).getMobileUserId();
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
            sysMomentComments.get(i).setSysMobileUser(sysMobileUser);

        }
//        List<SysParise> sysParises = sysPariseService.selectSysPariseMomentId(momentId);
        SysMomentComment sysMomentComment =new SysMomentComment();
        sysMomentComment.setMomentId(momentId);
        Integer momentCommentCount = sysMomentCommentService.selectCount(sysMomentComment);

        SysParise sysParise = new SysParise();
        sysParise.setMomentId(momentId);
        Integer momentCount = sysPariseService.selectCount(sysParise);
        Map<String, Object> map = new HashMap<>();
        map.put("sysMoment", sysMoment);
        map.put("sysMomentFiles", sysMomentFiles);
        map.put("sysMomentComments", sysMomentComments);
        map.put("momentCommentCount", momentCommentCount);
        map.put("momentCount", momentCount);
//        map.put("sysParises", sysParises);
        return AjaxResult.success(map);
    }

}
